package com.czy.seed.mvc.util;

import com.czy.seed.mvc.home.controller.NoticeController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @Author: liuyun
 * Created by liuyun  14:53 2018/1/4.
 */
public class MailUtil {


    public static Logger logger = LoggerFactory.getLogger(MailUtil.class);



    @SuppressWarnings("static-access")
    public boolean sendMessage(String fromTo, String subject, String messageText,SystemProperties systemProperties) {
        try {
            // 第一步：配置javax.mail.Session对象
            logger.info("为" + systemProperties.getSmtpHost() + "配置mail session对象");
            Properties props = new Properties();
            props.put("mail.smtp.host", systemProperties.getSmtpHost());
            //使用 STARTTLS安全连接
            props.put("mail.smtp.starttls.enable", "true");
            //google使用465或587端口
            //props.put("mail.smtp.port", "25");
            // 使用验证
            props.put("mail.smtp.auth", "true");
            //props.put("mail.debug", "true");
            Session mailSession = Session.getInstance(props, new MyAuthenticator(systemProperties.getMailFrom(), systemProperties.getPassword()));
            // 第二步：编写消息
            logger.info("编写消息from——to:" + systemProperties.getMailFrom() + "——" + fromTo);
            InternetAddress fromAddress = new InternetAddress(systemProperties.getMailFrom());
            InternetAddress toAddress = new InternetAddress(fromTo);

            MimeMessage message = new MimeMessage(mailSession);

            message.setFrom(fromAddress);
            message.addRecipient(RecipientType.TO, toAddress);

            message.setSentDate(Calendar.getInstance().getTime());
            if (subject.equals("") || subject == null) {
                message.setSubject(systemProperties.getSubject());
            } else {
                message.setSubject(subject);

            }
            message.setContent(messageText, systemProperties.getMessageType());
            // 第三步：发送消息
            Transport.send(message, message.getRecipients(RecipientType.TO));
            logger.info("邮件发送成功！");
        } catch (Exception e) {
            logger.error("邮件发送失败！");
            return false;
        }
        return true;

    }

}

class MyAuthenticator extends Authenticator {
    String userName = "";
    String password = "";

    public MyAuthenticator(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
}
